<?php
/*
require_once('site_config.php');
*/
require_once('/home/itrial/www/appconf.php');
// Usage Sample:
//require_once('CommonUtil.php');
//require_once('Captcha.class.php');
//require_once('Session.class.php');
//
//$captcha = new Captcha();
//$captcha->set_param(array('image_size'=>'104x28', 'padding'=>'2,15'));
//$number = $captcha->output_image();
//$sess = new Session();
//$sess->set('Captcha', $number);
//
// Change log:
//   add set_param function for setting image size, padding etc

class CMS_Captcha{
  private $min = 12134;
  private $max = 98762;
  private $site_data_path;
  
  private $font_path = null; # font path
  
  private $font_size = 18; # font path
  private $left_padding    = 20; 
  private $right_padding   = 20; 
  private $top_padding     = 20; 
  private $bottom_padding  = 20; 
  private $bg_color   = 'ffffff';
  private $text_color = 'aa7744';
  private $line_color  ='000000';
  private $image_size = '107x59';
  
  function __construct(){
    global $site_global_config;
    $this->site_data_path = $site_global_config['DATA'];
  }
  
  function set_param($h_param){
    if (isset($h_param['font_path']) ){ # must be relative to data path
      $this->font_path = $this->site_data_path.$h_param['font_path'];
    } 
    
    if (isset($h_param['font_size']) ){
      $this->font_size = $h_param['font_size'];
    } 
    
    if (isset($h_param['text_color']) ){
      $this->text_color = $h_param['text_color'];
    } 
    
    if (isset($h_param['line_color']) ){
      $this->line_color = $h_param['line_color'];
    } 
    
    if (isset($h_param['bg_color']) ){
      $this->bg_color = $h_param['bg_color'];
    }     
    
//   padding:25px 50px 75px 100px; (top, right, bottom, left)
//    padding:25px 50px 75px;
//        top padding is 25px
//        right and left paddings are 50px
//        bottom padding is 75px
//    padding:25px 50px;
//        top and bottom paddings are 25px
//        right and left paddings are 50px
//
//    padding:25px;
//        all four paddings are 25px

    if (isset($h_param['padding']) ){
      $temp_padding = $h_param['padding'];
      $pos = strpos($temp_padding, ','); # comma in key?
      if ($pos === false){ # single key, get rmc message count for $mid
        $this->left_padding   = $temp_padding;
        $this->right_padding  = $temp_padding;
        $this->top_padding    = $temp_padding;
        $this->bottom_padding = $temp_padding;
      } else {
        $a_padding = explode(',', $temp_padding);
        $padding_count = count($a_padding);
        if ($padding_count == 2){
          $this->top_padding    = $a_padding[0];
          $this->right_padding  = $a_padding[1];
          $this->bottom_padding = $a_padding[0];
          $this->left_padding   = $a_padding[1];
        } else if ($padding_count == 3)  { 
          $this->top_padding    = $a_padding[0];
          $this->right_padding  = $a_padding[1];
          $this->bottom_padding = $a_padding[2];
          $this->left_padding   = $a_padding[1];
        } else {  # 4 or more
          $this->top_padding    = $a_padding[0];
          $this->right_padding  = $a_padding[1];
          $this->bottom_padding = $a_padding[2];
          $this->left_padding   = $a_padding[3];
        }
        
      }
      
    } 
    
    if (isset($h_param['image_size']) ){
      $this->image_size = $h_param['image_size'];
    } 
  } 
  
  function random_number(){
    return mt_rand($this->min, $this->max);
  }
  
  
  // helper function for colors
  function makeRGBColor( $color, $image ){
    #return  imagecolorallocate( $image, 120, 120, 120 );
    
    $color = str_replace( "#", "", $color );
    $red = hexdec( substr( $color, 0, 2 ) );
    $green = hexdec( substr( $color, 2, 2 ) );
    $blue = hexdec( substr( $color, 4, 2 ) );
    $out = imagecolorallocate( $image, $red, $green, $blue );
    return $out ;
  }
  
  function output_image($word=''){    
    
    if ($word == '') 
      $word = $this->random_number();
    header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
    header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
    header( 'Content-Type:image/png' );
    echo $this->gen_image($word); 

    return $word; 
  }
  

  # @param: $word
  #
  function gen_image($word){
    
    // use any ttf font on your system
    // for our example we use the LucidaBright font from the Java distribution
    // you may also find TTF fonts in /usr/X11R6/lib/X11/fonts/TTF
    if ($this->font_path == null){
      $font = $this->site_data_path.'font/arial.ttf' ;
    } else {
      $font     = $this->font_path;
    }
    $fontSize = $this->font_size;

# don't know why standard path is not readable
$font = '/home/sales_nmplus/lib/CMS/arial.ttf';
    
    #echo "Padding: $padding<BR>\n";  
    // geometry -- build a box for word dimensions in selected font and size
    $wordBox = imageftbbox( $fontSize, 0, $font, $word );
        
    // x coordinate of UR corner of word
    $wordBoxWidth = $wordBox[2];
    
    // y coordinate of UL corner + LL corner of word
    $wordBoxHeight   = $wordBox[1] + abs( $wordBox[7] );
    $containerWidth  = $wordBoxWidth + ( $this->left_padding +  $this->right_padding);
    $containerHeight = $wordBoxHeight + ( $this->top_padding +  $this->bottom_padding );
    $textX = $this->left_padding;
    
#   echo  "$containerWidth, $containerHeight";
    
    // y coordinate of LL corner of word
    $textY = $containerHeight - $this->top_padding;
    
    // create the image
    //$captchaImage = imagecreate( $containerWidth, $containerHeight );
    
    list($image_w, $image_h) = explode('x', $this->image_size);
    $captchaImage = imagecreate($image_w, $image_h);
    
    // colors
    $backgroundColor = $this->makeRGBColor( $this->bg_color, $captchaImage );
    imagefill ( $captchaImage, 0, 0, $backgroundColor );
    $textColor       = $this->makeRGBColor( $this->text_color, $captchaImage );
        
    // add text
    imagefttext( $captchaImage, $fontSize, 0, $textX, $textY, $textColor, $font, $word );
    
    #echo " $fontSize, 0, $textX, $textY, $textColor, $font, $word";
  
    // rotate
#   $angle = rand( -20,20 );
#   $captchaImage = imagerotate( $captchaImage, $angle, $backgroundColor );
    
    // add lines
    $line = $this->makeRGBColor( $this->line_color, $captchaImage );
    
    for ( $i = 0; $i < 5; $i++ ) {
      $xStart = mt_rand( 0, $containerWidth );
      $yStart = mt_rand( 0, $containerHeight );
      $xEnd   = mt_rand( 0, $containerWidth  );
      $yEnd   = mt_rand( 0, $containerHeight );
      imageline( $captchaImage, $xStart, $yStart, $xEnd, $yEnd, $line );
    } # end of gen image function

    ob_start();
    imagePNG($captchaImage);
    $imagevariable = ob_get_contents();
    ob_end_clean();
    imagedestroy ($captchaImage);
    return $imagevariable;
} 
    
  
}
?>
